package microservicedemo.borrowservice.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import microservicedemo.borrowservice.mapper.BorrowMapper;
import microservicedemo.borrowservice.po.BorrowRecord;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.Period;
import java.util.List;

@AllArgsConstructor
@RestController
@Slf4j
public class BorrowController {

    private final BorrowMapper borrowMapper;

    @PostMapping("/borrowBook")
    public void borrowBook(@RequestBody BorrowRecord borrowRecord) {
        System.out.println(borrowRecord.getBorrowbook());
        borrowRecord.setBorrowDate(LocalDate.now());
        borrowRecord.setReturnDate(LocalDate.now().plus(Period.of(0,0,30)));
        borrowRecord.setStatus("已借出");
        borrowMapper.borrowBook(borrowRecord);
    }

    @PostMapping("/returnBook")
    public boolean returnBook(@RequestBody BorrowRecord borrowRecord) {
        borrowRecord.setStatus("已归还");
        borrowMapper.returnBook(borrowRecord);
        return true;
    }

    @GetMapping("/getOverdueBook")
    public List<BorrowRecord> getOverdueBook(){
        LocalDate now = LocalDate.now();
        return borrowMapper.getOverdueBook(now);
    }
}
